﻿using ST.Library.UI.NodeEditor;
using STNode_A2B.DataType;
using STNode_A2B.STNodeControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinNodeEditorDemo.Blender;

namespace STNode_A2B.STNodes
{
    [STNode("A2B")]
    public class STNode_Master_Node : STNode
    {
        private STNodeOption m_op_in_IIC;
        private STNodeOption m_op_in_Rx0;
        private STNodeOption m_op_in_Rx1;
        private STNodeOption m_op_in_Tx0;
        private STNodeOption m_op_in_Tx1;
        private STNodeOption m_op_out_B;

        private STNodeControl_CheckBox m_ctrl_CheckBox_IIC;
        private STNodeControl_CheckBox m_ctrl_CheckBox_Rx0;
        private STNodeControl_CheckBox m_ctrl_CheckBox_Rx1;
        private STNodeControl_CheckBox m_ctrl_CheckBox_Tx0;
        private STNodeControl_CheckBox m_ctrl_CheckBox_Tx1;

        private STNodeSelectEnumBox m_ctrl_SelectBox;

        private STNodeControl_Label m_ctrl_lbl_Up_ch;
        private STNodeControl_Label m_ctrl_lbl_Dn_ch;

        private STNodeControl_TextBox m_ctrl_textbox_Up_ch;
        private STNodeControl_TextBox m_ctrl_textbox_Dn_ch;


        private bool _isCheckedIIC;
        [STNodeProperty("IIC", "This is IIC checkbox")]
        public bool IsCheckedIIC
        {
            get { return _isCheckedIIC; }
            set { _isCheckedIIC = value; m_ctrl_CheckBox_IIC.Checked = value; }
        }

        private bool _isCheckedTx0;
        [STNodeProperty("Tx0", "This is Tx0 checkbox")]
        public bool IsCheckedTx0
        {
            get { return _isCheckedTx0; }
            set { _isCheckedTx0 = value; m_ctrl_CheckBox_Tx0.Checked = value; }
        }

        private bool _isCheckedTx1;
        [STNodeProperty("Tx1", "This is Tx1 checkbox")]
        public bool IsCheckedTx1
        {
            get { return _isCheckedTx1; }
            set { _isCheckedTx1 = value; m_ctrl_CheckBox_Tx1.Checked = value; }
        }

        private bool _isCheckedRx0;
        [STNodeProperty("Rx0", "This is Tx0 checkbox")]
        public bool IsCheckedRx0
        {
            get { return _isCheckedRx0; }
            set { _isCheckedRx0 = value; m_ctrl_CheckBox_Rx0.Checked = value; }
        }

        private bool _isCheckedRx1;
        [STNodeProperty("Rx1", "This is Tx1 checkbox")]
        public bool IsCheckedRx1
        {
            get { return _isCheckedRx1; }
            set { _isCheckedRx1 = value; m_ctrl_CheckBox_Rx1.Checked = value; }
        }

        public enum ChipType
        {
            NCA2428,
            NCA2418
        }

        private ChipType _chipType;
        [STNodeProperty("ChipType", "ChipType")]
        public ChipType ChipMixType
        {
            get { return _chipType; }
            set
            {
                _chipType = value;
                m_ctrl_SelectBox.Enum = value;
                Invalidate();
            }
        }

        private int _Up_ch_Data;
        [STNodeProperty("Up-ch", "Up-ch Data")]
        public string Up_ch_Data
        {
            get { return _Up_ch_Data.ToString(); }
            set 
            {
                int temp;
                if(int.TryParse(value, out temp))
                {
                    m_ctrl_textbox_Up_ch.EditorText = value;
                    _Up_ch_Data = int.Parse(value);
                    Invalidate();
                }
                
            }
        }

        private int _Dn_ch_Data;
        [STNodeProperty("Dn-ch", "Dn-ch Data")]
        public string Dn_ch_Data
        {
            get { return _Dn_ch_Data.ToString(); }
            set
            {
                int temp;
                if(int.TryParse(value,out temp))
                {
                    m_ctrl_textbox_Dn_ch.EditorText = value;
                    _Dn_ch_Data = int.Parse(value);
                    Invalidate();                   
                }

            }
        }

        private string _nodeName = "Master";
        [STNodeProperty("Title", "Title")]
        public string NodeName
        {
            get { return _nodeName; }
            set
            {
                this._nodeName = value;
                this.Title = this._nodeName;
            }
        }
        protected override void OnCreate()
        {
            base.OnCreate();
            this.TitleColor = Color.FromArgb(200, Color.DarkKhaki);
            this.Title = "Master Node";
            this.AutoSize = false;
            this.Size = new Size(180, 120);

            m_op_in_IIC = this.InputOptions.Add("",typeof(NoneType),true);
            m_op_in_Rx0 = this.InputOptions.Add("", typeof(NoneType), true);
            m_op_in_Rx1 = this.InputOptions.Add("",typeof(NoneType), true);
            m_op_in_Tx0 = this.InputOptions.Add("", typeof(NoneType), true);
            m_op_in_Tx1 = this.InputOptions.Add("", typeof(NoneType), true);

            m_op_out_B = this.OutputOptions.Add("B", typeof(AB), false);

            m_ctrl_CheckBox_IIC = new STNodeControl_CheckBox();
            m_ctrl_CheckBox_IIC.Text = "I2C";
            m_ctrl_CheckBox_IIC.DisplayRectangle = new Rectangle(25, 0, 100, 20);
            m_ctrl_CheckBox_IIC.ValueChanged += (s, e) =>
            {
                this._isCheckedIIC = m_ctrl_CheckBox_IIC.Checked;
                if (!this._isCheckedIIC)
                {
                    m_op_in_IIC.DisConnectionAll();
                    m_op_in_IIC.DataType = typeof(NoneType);
                    m_op_in_IIC.DotColor = Color.Gray;
                }
                else
                {
                    m_op_in_IIC.DataType = typeof(IIC);
                    m_op_in_IIC.DotColor = Color.DarkCyan;
                }
            };
            this.Controls.Add(m_ctrl_CheckBox_IIC);

            m_ctrl_CheckBox_Rx0 = new STNodeControl_CheckBox();
            m_ctrl_CheckBox_Rx0.Text = "Rx0";
            m_ctrl_CheckBox_Rx0.DisplayRectangle = new Rectangle(25, 20, 100, 20);
            m_ctrl_CheckBox_Rx0.ValueChanged += (s, e) =>
            {
                this._isCheckedRx0 = m_ctrl_CheckBox_Rx0.Checked;
                if (!this._isCheckedRx0)
                {
                    m_op_in_Rx0.DisConnectionAll();
                    m_op_in_Rx0.DataType = typeof(NoneType);
                    m_op_in_Rx0.DotColor = Color.Gray;
                }
                else
                {
                    m_op_in_Rx0.DataType = typeof(Tx0Rx0);
                    m_op_in_Rx0.DotColor = Color.DarkBlue;
                }
            };
            this.Controls.Add(m_ctrl_CheckBox_Rx0);

            m_ctrl_CheckBox_Rx1 = new STNodeControl_CheckBox();
            m_ctrl_CheckBox_Rx1.Text = "Rx1";
            m_ctrl_CheckBox_Rx1.DisplayRectangle = new Rectangle(25, 40, 100, 20);
            m_ctrl_CheckBox_Rx1.ValueChanged += (s, e) =>
            {
                this._isCheckedRx1 = m_ctrl_CheckBox_Rx1.Checked;
                if (!this._isCheckedRx1)
                {
                    m_op_in_Rx1.DisConnectionAll();
                    m_op_in_Rx1.DataType = typeof(NoneType);
                    m_op_in_Rx1.DotColor = Color.Gray;
                }
                else
                {
                    m_op_in_Rx1.DataType = typeof(Tx1Rx1);
                    m_op_in_Rx1.DotColor = Color.SkyBlue;
                }
            };
            this.Controls.Add(m_ctrl_CheckBox_Rx1);

            m_ctrl_CheckBox_Tx0 = new STNodeControl_CheckBox();
            m_ctrl_CheckBox_Tx0.Text = "Tx0";
            m_ctrl_CheckBox_Tx0.DisplayRectangle = new Rectangle(25, 60, 100, 20);
            m_ctrl_CheckBox_Tx0.ValueChanged += (s, e) =>
            {
                this._isCheckedTx0 = m_ctrl_CheckBox_Tx0.Checked;
                if (!this._isCheckedTx0)
                {
                    m_op_in_Tx0.DisConnectionAll();
                    m_op_in_Tx0.DataType = typeof(NoneType);
                    m_op_in_Tx0.DotColor = Color.Gray;
                }
                else
                {
                    m_op_in_Tx0.DataType = typeof(Rx0Tx0);
                    m_op_in_Tx0.DotColor = Color.LightPink;
                }
            };
            this.Controls.Add(m_ctrl_CheckBox_Tx0);

            m_ctrl_CheckBox_Tx1 = new STNodeControl_CheckBox();
            m_ctrl_CheckBox_Tx1.Text = "Tx1";
            m_ctrl_CheckBox_Tx1.DisplayRectangle = new Rectangle(25, 80, 100, 20);
            m_ctrl_CheckBox_Tx1.ValueChanged += (s, e) =>
            {
                this._isCheckedTx1 = m_ctrl_CheckBox_Tx1.Checked;
                if (!this._isCheckedTx1)
                {
                    m_op_in_Tx1.DisConnectionAll();
                    m_op_in_Tx1.DataType = typeof(NoneType);
                    m_op_in_Tx1.DotColor = Color.Gray;
                }
                else
                {
                    m_op_in_Tx1.DataType = typeof(Rx1Tx1);
                    m_op_in_Tx1.DotColor = Color.DeepPink;
                }
            };
            this.Controls.Add(m_ctrl_CheckBox_Tx1);

            m_ctrl_SelectBox = new STNodeSelectEnumBox();
            m_ctrl_SelectBox.DisplayRectangle = new Rectangle(80, 80, 90, 18);
            
            m_ctrl_SelectBox.Enum = this._chipType;
            m_ctrl_SelectBox.ValueChanged += (s, e) =>
            {
                this._chipType = (ChipType)m_ctrl_SelectBox.Enum;
            };
            this.Controls.Add(m_ctrl_SelectBox);

            m_ctrl_lbl_Up_ch = new STNodeControl_Label();
            m_ctrl_lbl_Up_ch.DisplayRectangle = new Rectangle(80, 0, 80, 18);
            m_ctrl_lbl_Up_ch.EditorText = "Up-ch";
            this.Controls.Add(m_ctrl_lbl_Up_ch);

            m_ctrl_textbox_Up_ch = new STNodeControl_TextBox();
            m_ctrl_textbox_Up_ch.DisplayRectangle = new Rectangle(110,20,40,18);
            m_ctrl_textbox_Up_ch.EditorText = Up_ch_Data;
            this.Controls.Add(m_ctrl_textbox_Up_ch);

            m_ctrl_lbl_Dn_ch = new STNodeControl_Label();
            m_ctrl_lbl_Dn_ch.DisplayRectangle = new Rectangle(80,40,80,18);
            m_ctrl_lbl_Dn_ch.EditorText = "Dn-ch";
            this.Controls.Add(m_ctrl_lbl_Dn_ch);

            m_ctrl_textbox_Dn_ch = new STNodeControl_TextBox();
            m_ctrl_textbox_Dn_ch.DisplayRectangle = new Rectangle(110, 60, 40, 18);
            m_ctrl_textbox_Dn_ch.EditorText = Dn_ch_Data;   
            this.Controls.Add(m_ctrl_textbox_Dn_ch);
        }

        protected override Point OnSetOptionDotLocation(STNodeOption op, Point pt, int nIndex)
        {
            if (op == m_op_out_B) return new Point(pt.X, pt.Y + 40);
            return base.OnSetOptionDotLocation(op, pt, nIndex);
        }
        protected override Rectangle OnSetOptionTextRectangle(STNodeOption op, Rectangle rect, int nIndex)
        {
            if (op == m_op_out_B) return new Rectangle(rect.X, rect.Y + 40, rect.Width, rect.Height);
            return base.OnSetOptionTextRectangle(op, rect, nIndex);
        }

        protected override void OnOwnerChanged()
        {
            //base.OnOwnerChanged();
            //if(this.Owner != null)
            //{
            //    this.Owner.SetTypeColor(typeof(AB), Color.Red);
            //}
            
        }
    }
}
